<template>
  <div>
    <el-container class="header">
      <!-- 头 -->
      <el-header class="father">
        <div></div>
        <!-- 新增用户表单 -->
        <el-button
          class="added"
          type="success"
          icon="el-icon-edit"
          @click="openDrawer('add')"
          >添加菜单</el-button
        >
        <el-dialog
          :title="drawerTitle"
          :visible.sync="dialogFormVisible"
          width="520px"
        >
          <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="权限组名称" label-width="120px">
              <el-radio-group v-model="form.is_point">
                <el-radio label="菜单"></el-radio>
                <el-radio label="权限点"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="活动区域" label-width="120px">
              <el-select v-model="form.pid" placeholder="请选择">
                <el-option
                  v-for="item in tableData"
                  :key="item.id"
                  :label="item.title"
                  :value="item.title"
                ></el-option> </el-select
            ></el-form-item>
            <el-form-item label="权限代码" label-width="120px" prop="code">
              <el-input v-model="form.code" style="width: 280px"></el-input>
            </el-form-item>
            <el-form-item label="权限标题" label-width="120px" prop="title">
              <el-input v-model="form.title" style="width: 280px"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" style="margin-right: 190px">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </div>
        </el-dialog>
      </el-header>

      <!-- 身体 -->
      <el-main>
        <!-- table表格数据区域 -->
        <el-table
          :data="tableData"
          row-key="id"
          style="width: 100%"
          default-expand-all
          v-loading="loading"
          :tree-props="{ children: 'childs' }"
        >
          <el-table-column prop="title" label="标题" width="180" align="center">
          </el-table-column>
          <el-table-column prop="code" label="权限点代码" align="center">
          </el-table-column>
          <el-table-column label="操作" width="120" align="center">
            <!-- 表格子项的作用域插槽 -->
            <template #default="{ row }">
              <div>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  plain
                  circle
                  @click="openDrawer('edit', row.id)"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  plain
                  circle
                  @click="del(row.id)"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { list, add, update, detail, remove } from '@/api/base/menus.js'

export default {
  data () {
    return {
      loading: false,
      dialogFormVisible: false,
      drawerType: 'add',
      form: {
        is_point: '',
        pid: '',
        code: '',
        title: ''
      },
      id: 0,
      // 验证规则
      rules: {
        code: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }]
      },
      tableData: []
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    // 获取table数据
    async initTable () {
      this.loading = true
      const { data } = await list(this.queryObj)
      console.log(data)
      this.tableData = data
      this.loading = false
    },

    // 提交表单
    async submit () {
      try {
        await this.$refs.form.validate()
        if (this.drawerType === 'add') {
          // 发起添加请求
          this.form.is_point = this.form.is_point !== '菜单'
          await add(this.form)
        }
        if (this.drawerType === 'edit') {
          // 发起修改请求
          this.form.is_point = this.form.is_point !== '菜单'
          await update({ id: this.id, ...this.form })
        }
        this.$refs.form.resetFields()
        // 关闭表单框
        this.dialogFormVisible = false

        // 提示用户
        this.$message.success(
          this.drawerType === 'add' ? '添加成功' : '修改成功'
        )

        // 重新获取新的数据渲染页面
        this.initTable()
      } catch (error) {
        this.$message.error('请输入必填项')
      }
    },

    // 打开编辑功能
    async openDrawer (type, id) {
      this.drawerType = type
      this.dialogFormVisible = true
      if (type === 'edit') {
        this.id = id
        const { data } = await detail({ id })
        this.form.title = data.title
        this.form.code = data.code
        this.form.pid = this.tableData.filter(
          (item) => item.id === data.id
        )[0].code
      }
    },

    // 删除功能
    async del (id) {
      try {
        await remove({ id })
        this.$message.success('删除成功')
        this.initTable()
      } catch (error) {
        this.$message.error('删除失败')
      }
    }
  },
  computed: {
    drawerTitle () {
      if (this.drawerType === 'add') return '创建用户'
      if (this.drawerType === 'edit') return '编辑用户'
      else {
        return 'add'
      }
    }
  }
}
</script>

<style scoped lang="less">
.header {
  background-color: #fff;
  margin: 10px 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 20px #ccc;
  .father {
    display: flex;
    justify-content: space-between;
    padding: 20px 20px;
    height: 100px;
    .added {
      height: 36px;
      width: 96px;
      font-size: 12px;
      padding-left: 11px;
    }
  }
}
</style>
